<?php
/**
 * Created by PhpStorm.
 * User: jsq
 * Date: 2017/5/3 0003
 * Time: 14:05
 */
namespace app\admin\controller;
use app\common\model\Leibie;
use app\common\model\Qudaoduanxin;
use app\common\traits\SendMessage;
use think\Db;
use think\Request;
use app\common\traits\Page;
use app\common\model\Order;
class Kcgl extends Admin
{
    use Page;
    use SendMessage;
    public function index(Request $request)
    {
        $leib = new Qudaoduanxin();
        $map = $request->param();
        $this->assign('form',$map);
        $data = $leib->map($map);
        $list   = $this->pageshow('qudaoduanxin',$data,'','','',200);
        $this->assign('list',$list);
        $yiyuan  =  $leib->yiyuan();
        $this->assign('yiyuan',$yiyuan);
        $kecheng  =  $leib->kecheng();
        $this->assign('kecheng',$kecheng);
        return $this->fetch('index');
    }

    public function add(Request $request){
        $leib = new Qudaoduanxin();
        if($request->isPost()) {
            $data = $request->param();
            $data['status'] = 1;
            $data['createtime'] = time();
            $resul = $leib->edit($data);
            return $resul;
        }else{
            $yiyuan  =  $leib->yiyuan();
            $this->assign('yiyuan',$yiyuan);
            $kecheng  =  $leib->kecheng();
            $this->assign('kecheng',$kecheng);
            return $this->fetch('add');
        }
    }

    public function cat(Request $request){
        $leib = new qudaoduanxin();
        $id = $request->param( 'id');
        $info   =  $leib->where('id',$id)->find();
        $this->assign('info',$info);
        $leibie = $leib->getleibei();
        $this->assign('leibie',$leibie);
        return $this->fetch();
    }

    public function edit(Request $request){
        $leib = new Qudaoduanxin();
        $data = $request->param();
        if($request->isPost()) {
            $data['status'] = 1;
            $data['createtime'] = time();
            $resul = $leib->edit($data);
            return $resul;
        }else{
            $info = Qudaoduanxin::get($data['id']);
            $this->assign('info',$info);
            $yiyuan  =  $leib->yiyuan();
            $this->assign('yiyuan',$yiyuan);
            $kecheng  =  $leib->kecheng();
            $this->assign('kecheng',$kecheng);
            return $this->fetch('edit');
        }
    }

    /**  查看订单详细
     * @return mixed
     */
    /* 删除 */
    public function del(Request $request)
    {
        $id   = $request->param('id');
        $leib = new Qudaoduanxin();
        $data['id']    = $id;
        $re =  $leib->where($data)->delete();
        if($re){
            return ['status'=>1,'msg'=>"删除订单数据成功！"];
        }else{
            return ['status'=>0,'msg'=>"删除数据失败！"];
        }
    }
    public function QdSendmessage(Request $request){
        $order = new Order();
        $map = $request->param();
        session('map',$map);
        $map = session('map');
        if( !empty($map['maphone'])  ){//电话搜索
            $list = $order->where(['maphone|otherphone'=>$map['maphone'],'status'=>1])->select();
        }elseif ( !empty($map['otherphone']) ){//电话搜索
            $list = $order->where(['maphone|otherphone'=>$map['otherphone'],'status'=>1])->select();
        }else{
            $data = $order->maps($map);
            $list = $this->pageshow('order',$data,'','','qdyytkplmsg asc',50);
//            $list = $this->protectData($list);//加密
        }
        $this->assign('form',$map);
        $this->assign('list',$list);
        //发送短信session
        $mapfasong = session('msg');
        $this->assign('mapfasong',$mapfasong);
        $this->getSj();
        $leib = new Qudaoduanxin();
        $kecheng  =  $leib->kecheng();
        $this->assign('kecheng',$kecheng);
        return $this->fetch('qdsendmessage');
    }
    public function qdfssendmessage(){
        $data = $this->request->param();
        session('msg',$data);
        $data = session('msg');
        if(empty($data['ids'])){
            return ['msg'=>'订单不能为空',status=>0,'icon'=>2];
        }else{
            $qudaoduanxin = Db::name('qudaoduanxin')->where(['yiyuan'=>$data['yiyuan'],'kecheng'=>$data['kecheng'],'status'=>1])->find();
            $yiyuaninfo   = Db::name('leibie')->where(['id'=>$data['yiyuan'],'status'=>1,'leibie'=>3])->field('name')->find();
            $kecheng      = Db::name('leibie')->where(['id'=>$data['kecheng'],'status'=>1,'leibie'=>9])->field('name')->find();

            $msgdata['param'] = [
                'yiyuan'  => $yiyuaninfo['name'],
                'kecheng' => $kecheng['name'],
                'time'    => getMonth_Day($data['sktime'].$qudaoduanxin['time']),
                'tkdz'    => $qudaoduanxin['tkdz'],
                'phone'     => $qudaoduanxin['bmdh'],
            ];
            $msgdata['tempcode'] = 'SMS_76365015';
            $msgdata['sendid'] = session("admin_user.adminid");//日志用

            foreach ($data['ids'] as $id_maphone){
                $id_maphone = explode(',',$id_maphone);
                $msgdata['phone'] = $id_maphone[1];
                $msgdata['orderid'] = $id_maphone[0];//日志用
                $res = $this->sendSmss($msgdata);
                if($res){
                    Db::name('order')->where(['id'=>$id_maphone[0]])->setInc('qdyytkplmsg');

                    $log = $this->getLogData(1);
                    $log['order_id'] = $id_maphone[0];
                    $log['maphone']  = $id_maphone[1];
                    Db::name('order_log')->insert($log);
                }
            }
            if($res){
                return ['msg'=>'发送成功',status=>2,'icon'=>1];
            }else{
                return ['msg'=>'发送失败',status=>1,'icon'=>2];
            }
        }
    }


    public function getLogData($khd=1){
        $log['operation'] = 16;
        $log['khd'] = $khd;
        $log['ip'] = request()->ip();
        $log['cztime'] = time();
        $data['czry'] = session("admin_user.adminid");
        return $log;
    }

    //根据医院动态获取课程
    public function getKecheng(){
        $yiyuan  = $this->request->param('yiyuan');
        if(!empty($yiyuan)){
            $kecheng_id = Db::name('qudaoduanxin')->where(['status'=>1,'yiyuan'=>$yiyuan])->column('kecheng');
            $kecheng = Db::name('leibie')->where(['status'=>1,'leibie'=>9,'id'=>['in',$kecheng_id] ])->field(['id','name'])->select();
        }else{
            $kecheng = Db::name('leibie')->where(['status'=>1,'leibie'=>9])->field(['id','name'])->select();
        }
        return $kecheng;
    }


}